import instance from "./index";
import { ContentType, IRequestParams } from "./type";

/**
 * @param method 请求方法 get post delete put
 * @param url 请求url
 * @param data 请求参数
 * @param config 请求的配置
 * @returns {Promise} 返回一个Promise对象
 */
const createReq = ({
  method,
  url,
  data,
  config,
  contentType = ContentType.Application,
}: IRequestParams) => {
  method = method.toLowerCase();
  if (method == "post") {
    return instance.post(url, data, {
      ...config,
      headers: { "Content-Type": contentType },
    });
  } else if (method == "get") {
    return instance.get(url, {
      params: data,
      ...config,
      headers: { "Content-Type": contentType },
    });
  } else if (method == "delete") {
    return instance.delete(url, {
      params: data,
      ...config,
      headers: { "Content-Type": contentType },
    });
  } else if (method == "put") {
    return instance.put(url, data, { ...config });
  } else {
    console.error("没有配置的请求方法" + method);
    return Promise.reject(new Error("没有配置的请求方法"));
  }
  return Promise.reject(new Error("没有配置的请求方法"));
};
export default createReq;
